package com.shaodongbo.httpurlconnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.nfc.Tag;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int GET_IMG = 1001;
    private static final int GET_PHONE = 1002;
    private Button btn1;
    private Button btn2;
    private ImageView iv_img;
    private Handler handler;
    private EditText edt_phone;
    private TextView txt_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_img = findViewById(R.id.iv_img);
        btn1 = findViewById(R.id.btn1);
        btn2 =findViewById(R.id.btn2);
        edt_phone = findViewById(R.id.edt_phone);
        txt_result = findViewById(R.id.txt_result);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_IMG) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    iv_img.setImageBitmap(bitmap);
                    return true;
                } else if (msg.what == GET_PHONE) {
                    String result = (String) msg.obj;
                    txt_result.setText(result);
                    return true;
                }
                return false;
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone = edt_phone.getText().toString();
                        useHttpURLConnectionGET(phone);
                    }
                }).start();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone = edt_phone.getText().toString();
                        useHttpURLConnectionPOST(phone);
                    }
                }).start();
            }
        });
    }

    private void useHttpURLConnectionPOST(String phone) {
        String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
//        String phoneUrl = baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        try {
            URL url = new URL(baseUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            String body = "mobileCode=".concat(phone).concat("&userID=");
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            bufferedWriter.write(body);
            bufferedWriter.flush();
            bufferedWriter.close();

            int code = httpURLConnection.getResponseCode();
            Log.e(TAG, "code = " + code);
            InputStream inputStream = httpURLConnection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

//            Message message = Message.obtain();
//            message.what = GET_IMG;
//            message.obj = bitmap;
//            handler.sendMessage(message);
//            iv_img.setImageBitmap(bitmap);

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "stringBuffer = " + stringBuffer.toString());

            Message message = Message.obtain();
            message.what = GET_PHONE;
            message.obj = stringBuffer.toString();
            handler.sendMessage(message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void useHttpURLConnectionGET(String phone) {
        String strUrl = "http://www.baidu.com";
        String imgUrl = "http://jsxy.gdcp.cn/UploadFile/2/2019/4/20/2019420231157622.png";
        String imgUrl2 = "https://files1.porsche.com/filestore/image/multimedia/zh/992-c4s-modelimage-sideshot/model/46d27470-ee6f-11e8-bec8-0019999cd470;sG;twebp/porsche-model.webp";
        String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";

        String phoneUrl = baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");
        try {
            URL url = new URL(phoneUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            int code = httpURLConnection.getResponseCode();
            Log.e(TAG, "code = " + code);
            InputStream inputStream = httpURLConnection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

//            Message message = Message.obtain();
//            message.what = GET_IMG;
//            message.obj = bitmap;
//            handler.sendMessage(message);
//            iv_img.setImageBitmap(bitmap);

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "stringBuffer = " + stringBuffer.toString());

            Message message = Message.obtain();
            message.what = GET_PHONE;
            message.obj = stringBuffer.toString();
            handler.sendMessage(message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
